package com.gxa.saas.portal.controller.hhy;

import com.gxa.saas.core.entity.vo.hhy.ShopLoginVO;
import com.gxa.saas.core.utils.JwtUtil;
import com.gxa.saas.portal.oauth2.OAuth2Token;
import com.gxa.saas.portal.service.hhyservice.ShopLoginService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@ApiOperation("店铺")
@RestController
@RequestMapping("/shop")
@Validated
public class ShopLoginController {

    @Autowired
    private ShopLoginService shopLoginService;

    @ApiOperation("店铺登录")
    @PostMapping("/login")
    public Map login(@Validated  @RequestBody ShopLoginVO shopLoginVO){
        Map map1 = new HashMap();
        Integer login = shopLoginService.login(shopLoginVO);
        if (login!=null){
            String token = JwtUtil.sign(shopLoginVO.getShopName(),shopLoginVO.getShopPwd());
            Subject subject = SecurityUtils.getSubject();
            subject.login(new OAuth2Token(token));

            map1.put("code", 200);
            map1.put("msg", "登录成功");
            map1.put("data",token);

            return map1;
        }else {
            map1.put("code", 401);
            map1.put("msg", "登录失败");
            return map1;
        }
    }

}
